package com.excel.comp;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import com.excel.read.AllTable;
import com.excel.read.Config;
import com.excel.read.DBTable;
import com.excel.read.HTMLComp;
import com.excel.read.GridTable;

public class HTMLTable extends HTMLComp {
	
    public List<List<HTMLComp>> cellList = new ArrayList<List<HTMLComp>>();
    
 
    
	public HTMLTable() {
		
	}
	
	public int getColspans(int rowIndex, int colIndex, String [][] data) {
		int num = 1;
		for(int i=colIndex+1; i<data[rowIndex].length; i++) {
			String cellValue = data[rowIndex][i];
			if(cellValue==null || cellValue.equals("")) {
				num++;
			}else {				
				break;
			}
		}
		return num;
	}
	
	
	/**
	 * 取得按钮
	 */
//	public List<HTMLComp> getBtns() {
//		List<HTMLComp> aList2 = new ArrayList<HTMLComp>();
//		for(int i=0; i<cellList.size(); i++) {			
//			List<HTMLComp> aList = cellList.get(i);
//			for(int j=0; j<aList.size(); j++) {
//				HTMLComp cell = aList.get(j);
//				if(cell instanceof HTMLCell) {
//					HTMLComp aComp = ((HTMLCell)cell).comp;
//					if(aComp.type == this.BTN_TYPE) {
//						aList2.add(aComp);					
//					}
//				}else {
//					
//					if(cell.type == this.BTN_TYPE) {
//						aList2.add(cell);		
//						
//					}
//				}			
//			}
//			
//		}
//		return aList2;
//	}
	
	public void generateTale() {
		int aIndex = this.name.indexOf("[");
		this.tableName = this.name.substring(aIndex+1,  this.name.length()-1);
		AllTable allTable = Config.allTable;
		GridTable aTable = allTable.getForm(this.name);
		String [][] data = aTable.data;
		int rows = data.length;
		int cols = data[0].length;
		for(int i=0; i<rows; i++) {
			List<HTMLComp> aList = new ArrayList<HTMLComp>();			
			for(int j=0; j<cols; j++) {				
				String cellValue = data[i][j];									
				int type = getType(data[i][j] );
				if(type==this.HIDDEN_TYPE) {
					List<HTMLComp>  aList2 = new ArrayList();
					String strs[] = cellValue.split("#");
					int aSpan = getColspans(i, j, data);				
					j+= (aSpan-1);
					for(int k=0; k<strs.length; k++) {
						HiddenComp aComp = new HiddenComp();
						HashMap hm4 = getParameters(strs[k]);
						aComp.name = strs[k];
						aComp.setParamMap(hm4);
						aComp.tableName = this.tableName;
						aComp.type = this.HIDDEN_TYPE;
						aList.add(aComp);
					}
					
				}else {
					List<HTMLComp> aComps = getComps(data[i][j]);
					HTMLCell aCell = new HTMLCell();
					int aSpan = getColspans(i, j, data);
					aCell.colspan = aSpan;
					j+= (aSpan-1);
					aCell.comps = aComps;
					for(int k=0; k<aComps.size(); k++) {
						aComps.get(k).tableName = this.tableName;
					}					
					aList.add(aCell);		
				}
						
			}
			cellList.add(aList);
		}
	}
	
	public List getAllFields() {
		List aList2 = new ArrayList();
		for(int i=0; i<cellList.size(); i++) {			
			List<HTMLComp> aList = cellList.get(i);
			for(int j=0; j<aList.size(); j++) {
				HTMLComp cell = aList.get(j);
				if(cell instanceof HTMLCell) {
					List<HTMLComp> aComps = ((HTMLCell)cell).comps;
					
					for(int k=0; k<aComps.size(); k++) {
						HTMLComp aComp = aComps.get(k);
						if(aComp.type == this.INPUT_TYPE || aComp.type == this.HIDDEN_TYPE) {
							aList2.add(aComp.getVar());					
						}
					}
				}else {					
					if(cell.type == this.HIDDEN_TYPE) {
						aList2.add(cell.getVar());
					}
				}
			
			}
			
		}
		
		return aList2;
	}
	/**
	 * 单表更新函数
	 * @return
	 */
	public String getUpdateScript() {
		StringBuffer sbf = new StringBuffer();
		StringBuffer  parameterBuffer = new StringBuffer();
		
		sbf.append("<script>");
		
		
		
		
		
		sbf.append("function update"+this.tableName+"() {\r\n");		
		//取得所有的字段
		for(int i=0; i<cellList.size(); i++) {			
			List<HTMLComp> aList = cellList.get(i);
			for(int j=0; j<aList.size(); j++) {
				HTMLComp cell = aList.get(j);
				if(cell instanceof HTMLCell) {
					List<HTMLComp> aComps = ((HTMLCell)cell).comps;
					for(int k=0; k<aComps.size(); k++) {
						HTMLComp aComp = aComps.get(k);
						if(aComp.type == this.INPUT_TYPE || aComp.type == this.HIDDEN_TYPE) {
							String varStr = aComp.getVarStatement();
							parameterBuffer.append(varStr).append("\r\n");
						}
					}
				}else {					
					if(cell.type == this.HIDDEN_TYPE) {
						String varStr = cell.getVarStatement();
						parameterBuffer.append(varStr).append("\r\n");
					}
				}			
			}
			
		}
		
		sbf.append(parameterBuffer);
		AllTable allTable = Config.allTable;
		DBTable aTable = (DBTable)allTable.get(this.name);		
		//数据校验, 以及传递参数
		StringBuffer parameters = new StringBuffer();
		StringBuffer struct = new StringBuffer();
		struct.append("var struct = {\r\n");
		for(int i=0; i<cellList.size(); i++) {			
			List<HTMLComp> aList = cellList.get(i);
			for(int j=0; j<aList.size(); j++) {
				HTMLComp cell = aList.get(j);
				if(cell instanceof HTMLCell) {
					List<HTMLComp> aComps = ((HTMLCell)cell).comps;
					
					for(int k=0; k<aComps.size(); k++) {
						HTMLComp aComp = aComps.get(k);
						if(aComp.type == this.INPUT_TYPE || aComp.type == this.HIDDEN_TYPE) {
							String varStr = aComp.getVarName();
//							String value = aComp.getVarValue();
							String type = aTable.getType(aComp.getFieldName());
							boolean allowNull = aTable.allowNull(aComp.getFieldName());
							if(!allowNull) {
								sbf.append("if(!validateName(\""+aComp.getFieldName()+"\", "+aComp.getVarName()+")) {");
								sbf.append("return;");
								sbf.append("}");
							}
							parameters.append("+'&"+aComp.getFieldName()+"='+"+aComp.getVarName());
							struct.append(aComp.getFieldName()+":"+aComp.getVarName()+", \r\n");
						
						}
					}

				}else {					
					if(cell.type == this.HIDDEN_TYPE) {
						String varStr = cell.getVarStatement();					
						String value = cell.getVarValue();
						String type = aTable.getType(cell.getFieldName());
						boolean allowNull = aTable.allowNull(cell.getFieldName());
						if(!allowNull) {
							sbf.append("if(!validateName(\""+cell.getFieldName()+"\", "+cell.getVarName()+")) {");
							sbf.append("return;");
							sbf.append("}");
						}
						parameters.append("+'&"+cell.getFieldName()+"='+"+cell.getVarName());
						struct.append(cell.getFieldName()+":"+cell.getVarName()+", \r\n");
					}
				}
			
			}
			
		}
		

		struct.append("ok:1\r\n");
		struct.append("}\r\n");
		sbf.append(struct);
		sbf.append("var dom=doRequestUsingPOST(actionPath+'tree.do', 'dispatch=updateTable&maticTableName="+this.name+"'"+parameters+", true, _postupdate"+this.tableName+");");			
		sbf.append("function _postupdate"+this.tableName+"() {");
		sbf.append("postupdate"+this.tableName+"(dom, struct);");
		sbf.append("}");
		
		sbf.append("}\r\n");
		
		
		
		
		
		
		
		parameterBuffer = new StringBuffer();
		sbf.append("function new"+this.tableName+"() {\r\n");		
		//取得所有的字段
		for(int i=0; i<cellList.size(); i++) {			
			List<HTMLComp> aList = cellList.get(i);
			for(int j=0; j<aList.size(); j++) {
				HTMLComp cell = aList.get(j);
				if(cell instanceof HTMLCell) {
					List<HTMLComp> aComps = ((HTMLCell)cell).comps;
					for(int k=0; k<aComps.size(); k++) {
						HTMLComp aComp = aComps.get(k);
						if(aComp.type == this.INPUT_TYPE || aComp.type == this.HIDDEN_TYPE) {
							String varStr = aComp.getVarStatement();
							parameterBuffer.append(varStr).append("\r\n");
							
							parameterBuffer.append("if("+aComp.getVarName()+"==null || "+aComp.getVarName()+"==\"\") {");
							String defaultValue = aTable.getDefaultValue(aComp.getFieldName());
							
							String type =  aTable.getType(aComp.getFieldName());
							if(type.indexOf("VARCHAR")>-1) {
								parameterBuffer.append(""+aComp.getVarName()+" = \""+defaultValue+"\";").append("\r\n");
							}else if(type.indexOf("NUMBER")>-1) {
								
								parameterBuffer.append(""+aComp.getVarName()+" = \""+defaultValue+"\";").append("\r\n");
							}else{
								parameterBuffer.append(""+aComp.getVarName()+" = \""+defaultValue+"\";").append("\r\n");
							}								
							parameterBuffer.append("}");
							
							
							
						}
					}
				}else {					
					if(cell.type == this.HIDDEN_TYPE) {
						String varStr = cell.getVarStatement();
						parameterBuffer.append(varStr).append("\r\n");
						
						parameterBuffer.append("if("+cell.getVarName()+"==null || "+cell.getVarName()+"==\"\") {");
						String defaultValue = aTable.getDefaultValue(cell.getFieldName());
						String type =  aTable.getType(cell.getFieldName());
						if(type.indexOf("VARCHAR")>-1) {
							parameterBuffer.append(""+cell.getVarName()+" = \""+defaultValue+"\";").append("\r\n");
						}else if(type.indexOf("NUMBER")>-1) {
							parameterBuffer.append(""+cell.getVarName()+" = \""+defaultValue+"\";").append("\r\n");
						}else{
							parameterBuffer.append(""+cell.getVarName()+" = \""+defaultValue+"\";").append("\r\n");
						}								
						parameterBuffer.append("}");
						
					}
				}			
			}
			
		}	
		sbf.append(parameterBuffer);				
		//数据校验, 以及传递参数
		StringBuffer parameters2 = new StringBuffer();
		StringBuffer struct2 = new StringBuffer();
		struct2.append("var struct = {\r\n");
		for(int i=0; i<cellList.size(); i++) {			
			List<HTMLComp> aList = cellList.get(i);
			for(int j=0; j<aList.size(); j++) {
				HTMLComp cell = aList.get(j);
				if(cell instanceof HTMLCell) {
					List<HTMLComp> aComps = ((HTMLCell)cell).comps;
					
					for(int k=0; k<aComps.size(); k++) {
						HTMLComp aComp = aComps.get(k);
						if(aComp.type == this.INPUT_TYPE || aComp.type == this.HIDDEN_TYPE) {
							String varStr = aComp.getVarName();					
							String type = aTable.getType(aComp.getFieldName());
							boolean allowNull = aTable.allowNull(aComp.getFieldName());
							if(!allowNull) {
								if(!(this.tableName+"_ID").equals(aComp.getFieldName())) {
									sbf.append("if(!validateName(\""+aComp.getFieldName()+"\", "+aComp.getVarName()+")) {");
									sbf.append("return;");
									sbf.append("}");
								}
								
							}
							parameters2.append("+'&"+aComp.getFieldName()+"='+"+aComp.getVarName());
							struct2.append(aComp.getFieldName()+":"+aComp.getVarName()+", \r\n");
						
						}
					}

				}else {					
					if(cell.type == this.HIDDEN_TYPE) {
						String varStr = cell.getVarStatement();					
						String value = cell.getVarValue();
						String type = aTable.getType(cell.getFieldName());
						boolean allowNull = aTable.allowNull(cell.getFieldName());
						if(!allowNull) {
							if(!(this.tableName+"_ID").equals(cell.getFieldName())) {
								
								sbf.append("if(!validateName(\""+cell.getFieldName()+"\", "+cell.getVarName()+")) {");
								sbf.append("return;");
								sbf.append("}");
							}
							
						}
						parameters2.append("+'&"+cell.getFieldName()+"='+"+cell.getVarName());
						struct2.append(cell.getFieldName()+":"+cell.getVarName()+", \r\n");
					}
				}
			
			}
			
		}
		

		struct2.append("ok:1\r\n");
		struct2.append("}\r\n");
		sbf.append(struct2);
		sbf.append("var dom=doRequestUsingPOST(actionPath+'tree.do', 'dispatch=addTable2&maticTableName="+this.name+"'"+parameters2+", true, _postadd"+this.tableName+");");			
		sbf.append("function _postadd"+this.tableName+"() {");
		sbf.append("postadd"+this.tableName+"(dom, struct);");
		
		sbf.append("}");
		
		sbf.append("}\r\n");
		
		
		
		
		
		sbf.append("</script>");

		
		return sbf.toString();
	}
	
	public String getHTML() {
		StringBuffer sbf = new StringBuffer();
		sbf.append("<form id=\""+this.tableName+"\" name=\""+this.tableName+"\">");
		sbf.append("<table cellspacing=0 cellpadding=0 >");
		for(int i=0; i<cellList.size(); i++) {
			sbf.append("<tr>");
			List<HTMLComp> aList = cellList.get(i);
			for(int j=0; j<aList.size(); j++) {
				HTMLComp cell = aList.get(j);
				sbf.append(cell.getHTML());
			}
			sbf.append("</tr>");
		}
		sbf.append("</table>");
		sbf.append("</form>");
		
		
//		List<HTMLComp> 	aList  = getBtns();
		sbf.append("<script>");
		
		sbf.append("function "+this.tableName+"_Form() {");
		sbf.append("this.setNewState = function(obj) {");	
		sbf.append("this.clearForm();");
		sbf.append("//obj.innerText == \"修改\";");
		sbf.append("\r\n");
		sbf.append("document.getElementById(\""+this.tableName+".btn\").style.display = \"none\";");
		sbf.append("document.getElementById(\""+this.tableName+".btnNew\").style.display = \"block\";");
		//sbf.append("document.getElementById(\""+this.tableName+"."+this.tableName+"_ID\").style.display = \"block\";");
		sbf.append("};");
			
		sbf.append("this.setUpdateState = function(obj) {");				
		sbf.append("//obj.innerText == \"新增\";");
		sbf.append("\r\n");
		sbf.append("document.getElementById(\""+this.tableName+".btn\").style.display = \"block\";");
		sbf.append("document.getElementById(\""+this.tableName+".btnNew\").style.display = \"none\";");
		//sbf.append("document.getElementById(\""+this.tableName+"."+this.tableName+"_ID\").style.display = \"none\";");
		sbf.append("};");
		
		
		sbf.append("this.clearForm = function(obj) {");				
		sbf.append("\r\n");
		List listVar = this.getAllFields();
		for(int m=0; m<listVar.size(); m++) {
			sbf.append("document.getElementById(\""+listVar.get(m)+"\").value='';\r\n");
		}
		sbf.append("};");

		
		
		sbf.append("}");	
		
		
		
		

		
		
		
		
		
		
		
		
		
		sbf.append("var "+this.tableName+"= new "+this.tableName+"_Form();");
		sbf.append(this.tableName+".setNewState();");
		sbf.append("</script>");
		
		return sbf.toString();
	}
	
}